<template>
  <div class="container">
    <img src="../assets/images/logo.png" alt="">
    <h1>用户注册</h1>
    <el-form :model="form" :rules="rules" ref="form" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input type="password" v-model="form.confirmPassword" placeholder="请确认密码"></el-input>
      </el-form-item>
      <el-button type="primary" @click="submitForm">注册</el-button><br />
      <router-link to="/login" class="return-link">返回登录</router-link>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      form: {
        username: "",
        password: "",
        confirmPassword: "",
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" },],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confirmPassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          { validator: this.validateConfirmPassword, trigger: "blur" },
        ],
      },
    };
  },

methods: {
  submitForm() {
    // 通过 $refs.form.validate 方法对表单进行验证，如果验证通过（即 valid 为 true）
    this.$refs.form.validate((valid) => {
      if (valid) {
        this.$axios({
          method: "post",
          url: "http://106.52.94.26:3001/user/register",
          data: {
            username: this.form.username,
            password: this.form.password,
          },
        })
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: res.data.msg,
              type: "success",
            });
            window.sessionStorage.setItem("token", res.data.token);
            window.sessionStorage.setItem("user", JSON.stringify(res.data.user));
            this.form.username = "";
            this.form.password = "";
            this.form.confirmPassword = "";
          } 
        })
        .catch(() => {
          this.$message.error("该用户已存在，请换个用户名");
        });
      } else {
        this.$message.error("两次输入的密码不一致");
      }
    });
  },
  // validateConfirmPassword 是一个自定义的表单校验规则，用于验证确认密码和密码是否一致，如果不一致，将返回一个错误信息。
  validateConfirmPassword(rule, value, callback) {
    if (value !== this.form.password) {
      callback(new Error("两次输入的密码不一致"));
    } else {
      callback();
    }
  },
},

};
</script>

<style scoped>
body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
}

.container {
  max-width: 420px;
  height: 460px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 6px 20px 0 rgba(37, 43, 50, 0.07);
  border: 1px solid #f9f9f9;
  border-radius: 5px;
  margin-top: 100px;
  flex-direction: column;
  justify-content: space-evenly;

}

.container img {
  width: 120px;
  margin: 0 auto;
  margin-bottom: 10px;
  display: block;
}

h1 {
  text-align: center;
  margin-bottom: 10px;
}

.el-input:focus {
  border-color: #ff5c00;
  outline: none;
}

.el-form-item__content {
  margin-left: 0;
}

.el-button {
  width: 100%;
  margin-top: 20px;
  text-align: center;
}

.return-link {
  display: block;
  margin-top: 10px;
  color: #333;
}

.return-link:hover {
  color: #ff5c00;
}
</style>
